// pages/wx_zm_index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    recommend_book_list: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    book_classifiction: [{
      path: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      title:"男生榜"
    }, {
        path: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        title: "新作榜"
      }, {
        path: 'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
        title: "完结榜"
      }],
      recommendBookList:[],
    clientWidth:0
  },
  getClientWidth: function() {
    this.setData({
      clientWidth: app.globalData.clientWidth
    })
  },
  getRecommendBook: function() {
    var that = this
    
    wx.request({
      url: 'https://api.zhuishushenqi.com/book/5aa8e1f785a2d67ad252abe8/recommend',
      header: {
        'content-type': 'application/json'
      },
      success: function(res){
        for (var i = 0; i < res.data.books.length; i++){
          var bookUrl = decodeURIComponent(res.data.books[i].cover)
          res.data.books[i].cover = bookUrl.replace("/agent/","")
        }
        that.setData({
          recommendBookList: res.data.books
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getClientWidth()
    this.getRecommendBook()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("下拉刷新")
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  recommend_books:function(event){
    // console.log(JSON.stringify(event))
    if(event.detail.current == 3){

    }
  },
  toSearch: function(){
    console.log("触发搜索")
    //点击搜索，跳转到搜索页面
    wx.navigateTo({
      url: '/pages/wx_zm_search/search',
    })
  },
  toBookDetail: function(e) {
    if(e.target.dataset.itemid==undefined){
      return
    }

    wx.navigateTo({
      url: '/pages/wx_zm_book_detail/detail?id=' + e.target.dataset.itemid,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  }
})